const express = require("express");
const app = express();
const router = require("./router");
const morgan = require("morgan");

app.use(
  morgan(":method :url :status :res[content-length] - :response-time ms")
);
// 配置解析表单请求体：application/json
app.use(express.json());
// 解析表单请求体：application/x-www-form-urlencoded
app.use(express.urlencoded());

app.use("/api", router);

// 404
app.use((req, res, next) => {
  res.status(404).json({
    message: "404 Not Found",
  });
});

// 给所有中间件之后挂载错误处理中间件
app.use((err, req, res, next) => {
  res.status(500).json({
    error: err.message,
  });
});

app.listen(3000, () => {
  console.log("server is running at http://localhost:3000");
});
